package com.ruoyi.web.controller.item;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.item.domain.ItemGoods;
import com.ruoyi.item.service.IItemGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 商品管理Controller
 * 
 * @author ruoyi
 * @date 2022-03-10
 */
@Api("商品管理Controller")
@RestController
@RequestMapping("/item/goods")
public class ItemGoodsController extends BaseController
{
    @Autowired
    private IItemGoodsService itemGoodsService;

    /**
     * 查询商品管理列表
     */
    @ApiOperation("查询商品管理列表")
    @PreAuthorize("@ss.hasPermi('item:goods:list')")
    @GetMapping("/list")
    public TableDataInfo list(ItemGoods itemGoods)
    {
        startPage();
        List<ItemGoods> list = itemGoodsService.selectItemGoodsList(itemGoods);
        return getDataTable(list);
    }

    /**
     * 导出商品管理列表
     */
    @ApiOperation("导出商品管理列表")
    @PreAuthorize("@ss.hasPermi('item:goods:export')")
    @Log(title = "商品管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ItemGoods itemGoods)
    {
        List<ItemGoods> list = itemGoodsService.selectItemGoodsList(itemGoods);
        ExcelUtil<ItemGoods> util = new ExcelUtil<ItemGoods>(ItemGoods.class);
        util.exportExcel(response, list, "商品管理数据");
    }

    /**
     * 获取商品管理详细信息
     */
    @ApiOperation("获取商品管理详细信息")
    @PreAuthorize("@ss.hasPermi('item:goods:query')")
    @GetMapping(value = "/{goodsId}")
    public AjaxResult getInfo(@PathVariable("goodsId") Long goodsId)
    {
        return AjaxResult.success(itemGoodsService.selectItemGoodsByGoodsId(goodsId));
    }

    /**
     * 新增商品管理
     */
    @ApiOperation("新增商品管理")
    @PreAuthorize("@ss.hasPermi('item:goods:add')")
    @Log(title = "商品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ItemGoods itemGoods)
    {
        itemGoods.setCreateBy(getUsername());
        return toAjax(itemGoodsService.insertItemGoods(itemGoods));
    }

    /**
     * 修改商品管理
     */
    @ApiOperation("修改商品管理")
    @PreAuthorize("@ss.hasPermi('item:goods:edit')")
    @Log(title = "商品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ItemGoods itemGoods)
    {
        itemGoods.setUpdateBy(getUsername());
        return toAjax(itemGoodsService.updateItemGoods(itemGoods));
    }

    /**
     * 删除商品管理
     */
    @ApiOperation("删除商品管理")
    @PreAuthorize("@ss.hasPermi('item:goods:remove')")
    @Log(title = "商品管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{goodsIds}")
    public AjaxResult remove(@PathVariable Long[] goodsIds)
    {
        return toAjax(itemGoodsService.deleteItemGoodsByGoodsIds(goodsIds));
    }
}
